<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="500px"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm">
      <template #type="{ model, field }">
        <RadioGroup
          v-model:value="model[field]"
          option-type="button"
          button-style="solid"
          :options="productInStockTypeOptions"
        />
      </template>
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref, onMounted } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { inStockFormSchema } from './stock.data';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { inProductStockApi } from '/@/api/modules/business/product/stock';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getDictEntryWithListByDictCodeApi } from '/@/api/modules/system/dictEntry';
  import { RadioGroup } from 'ant-design-vue';
  export default defineComponent({
    name: 'InStockDrawer',
    components: { BasicDrawer, BasicForm, RadioGroup },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      const rowId = ref();
      const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
        labelWidth: 90,
        baseColProps: { span: 24 },
        schemas: inStockFormSchema,
        showActionButtonGroup: false,
      });
      const { createMessage } = useMessage();
      const productInStockTypeOptions = ref<any>([]);
      onMounted(async () => {
        productInStockTypeOptions.value = await getDictEntryWithListByDictCodeApi({
          dictCode: 'product_in_stock_type',
        });
      });

      const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
        resetFields();
        setDrawerProps({ confirmLoading: false });

        isUpdate.value = !!data?.isUpdate;
        // const productInStockTypeOptions = await getDictEntryWithListByDictCodeApi({
        //   dictCode: 'product_in_stock_type',
        // });
        // await updateSchema([
        //   {
        //     field: 'type',
        //     componentProps: {
        //       options: productInStockTypeOptions,
        //     },
        //   },
        // ]);
        if (unref(isUpdate)) {
          rowId.value = data.record.id;
          setFieldsValue({
            ...data.record,
          });
        }
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增入库' : '编辑入库'));

      async function handleSubmit() {
        try {
          const values = await validate();
          setDrawerProps({ confirmLoading: true });
          await inProductStockApi(values);
          createMessage.success('入库成功');
          closeDrawer();
          emit('success');
        } finally {
          setDrawerProps({ confirmLoading: false });
        }
      }

      return {
        registerDrawer,
        registerForm,
        getTitle,
        handleSubmit,
        productInStockTypeOptions,
      };
    },
  });
</script>
